package com.gph.saviorframework.uc;

import com.gph.saviorframework.Constants;
import com.gph.saviorframework.common.model.User;
import com.gph.saviorframework.poi.POIUtils;
import com.gph.saviorframework.security.service.OrgService;
import com.gph.saviorframework.security.service.UserService;
import com.gph.saviorframework.util.DateUtils;
import com.gph.saviorframework.util.SecUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel 文件解析器
 */
@Component
public class ExcelParser {

    @Autowired
    private UserService userService;

    @Autowired
    private OrgService orgService;

    public void parseUserExcelFile(String excelFile,String orgId) throws Exception{
        Workbook workbook = null;
        List<User> userList = new ArrayList<>();
        File file = new File(excelFile);
        if(file.exists()){
            if(file.isFile()){
                workbook = POIUtils.openWorkBook(file);
                //按照规则读取
                if(workbook!=null){
                    //获取到第一个worksheet
                    Sheet sheet = POIUtils.getSheetByNum(workbook,0);

                    int rowsNum = sheet.getPhysicalNumberOfRows();

                    User user = null;

                    for(int i = 1;i<rowsNum;i++){
                        user = new User();
                        //A列代表的是中文姓名
                        Row row = POIUtils.getRowByIndex(sheet, i);

                        user.setName(POIUtils.getCellValue(row, "B"));
                        user.setUsername(POIUtils.getCellValue(row, "Q"));
                        user.setEnabled(true);
                        user.setAccountNonExpired(true);
                        user.setAccountNonLocked(true);
                        if(POIUtils.getCellValue(row, "E").equals("男")){
                            user.setGender("M");
                        }
                        else{
                            user.setGender("F");
                        }
                        user.setCredentialsNonExpired(true);
                        user.setEmail(POIUtils.getCellValue(row, "Q"));
                        /*user.setTel(POIUtils.getCellValue(row, "M"));
                        user.setMobile(POIUtils.getCellValue(row, "N"));*/
                        user.setBirthday(DateUtils.stringToDate(POIUtils.getCellValue(row, "D"), "yyyy-MM-dd"));
                        user.setIdentity(POIUtils.getCellValue(row, "C"));

                        user.setOrder(0);
                        user.setVersion(0);

                        user.setPassword(SecUtils.getMD5(Constants.DEFAULT_PASSWORD.getBytes()));
                        user.setUserType("USER");//设置为普通用户

                        user.setOrg(orgService.get(orgId));

                        Date date = new Date();
                        user.setCreated(date);
                        user.setCreator(userService.get("admin"));

                        user.setModified(date);
                        user.setModifier(userService.get("admin"));

                        userList.add(user);
                    }
                }
            }
            else{
                throw new Exception("no file");
            }
        }
        userService.batchSave(userList);
    }
}
